package com.project.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.project.entity.VerifyEntity;
import com.project.util.Verifier;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CSCInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.err.println("request = " );
        String token = request.getHeader("token");
        if (token == null || token.length() == 0) {
            response.getWriter().print("noland");
            return false;
        }
        String verifierJwt = Verifier.verifierJwt(token);//拆分验证
        if ("".equals(verifierJwt)){
            response.getWriter().print("noland");
            return false;
        }

        VerifyEntity verifyEntity = JSONObject.parseObject(verifierJwt, VerifyEntity.class);
        LoginThreadLocal.saveLoginVerify(verifyEntity); //保存在线程副本
        return true;
    }
}
